import cv2
import os
import sys

PIC_PATH = './images/'

width, height = 100, 100

pic_file = os.listdir(PIC_PATH)
same_pic_index = []
imgs = []
has_same = set()

count = len(pic_file)

if count == 0:
    print('没有图像')
    sys.exit(0)

for file_name in pic_file:
    pic_name = PIC_PATH + file_name
    img = cv2.imread(pic_name)

    img = cv2.resize(img, (width, height))
    imgs.append(img)

for i in range(0, count - 1):
    if i in has_same:
        continue
    templ = imgs[i]
    same = [i]
    for j in range(0 + i + 1, count):
        if j in has_same:
            continue
        pic = imgs[j]
        results = cv2.matchTemplate(pic, templ, cv2.TM_CCOEFF_NORMED)
        if results > 0.9:
            same.append(j)
            has_same.add(i)
            has_same.add(j)
    if len(same) > 1:
        same_pic_index.append(same)

for same_list in same_pic_index:
    text = '相同的图片'
    for same in same_list:
        text += str(pic_file[same]) + ","
        print(text)
